package cl.blueprintsit.utils.bdd;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class LocalSQLRepository implements SQLRepository {

    private final String jdbcProtocol;
    private final String username;
    private final String password;
    private final String server;
    private final String port;
    private final String bddName;

    public LocalSQLRepository(String driverName, String jdbcProtocol, String server, String port,
                              String bddName, String username,
                              String password) throws ClassNotFoundException {

        Class.forName(driverName);

        this.server = server;
        this.port = port;
        this.bddName = bddName;
        this.jdbcProtocol = jdbcProtocol;
        this.username = username;
        this.password = password;
    }

    @Override
    public Connection getConnection() throws SQLException {

        String urlConnection = jdbcProtocol + ":@" + server + ":" + port + "/" + bddName;
        return DriverManager.getConnection(urlConnection, username, password);
    }

    @Override
    public void testConnection() throws SQLException {
        Connection connection = getConnection();
        connection.close();
    }
}
